package org.example.com.Demo;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class ZsetRedisDemo {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public void a(){
        ZSetOperations<String, String> zset = stringRedisTemplate.opsForZSet();
        zset.add("A","aa",9.8);
        zset.add("B","bb",5.4);
        zset.add("C","cc",1.4);
        zset.add("A","dd",11);
        zset.add("A","ee",2);
        zset.add("A","bb",2);
        zset.addIfAbsent("A","ff",4.7);
        zset.addIfAbsent("B","cc",4.22);
        Set<String> set1 = Set.of("aa","bb","cc","dd","ee","ff");
        Set<String> set2 = Set.of("aa","mm","33","cc","kk","ff");
        for (String s : set1) {
            zset.add("B",s,Math.random()*100);
        }
        for (String s : set2) {
            zset.add("C",s,Math.random()*100);
        }
    }
    public void b(){
        ZSetOperations<String, String> zset = stringRedisTemplate.opsForZSet();
        Set<String> set1 = zset.range("A",0,-1);
        Long size = zset.count("A",3,7);
        String v1=zset.randomMember("A");
        List<String> set2 = zset.randomMembers("A",3);
        Set<String> set3 = zset.distinctRandomMembers("A",3);
        log.info("set1:{}",set1);
        log.info("size:{}",size);
        log.info("v1:{}",v1);
        log.info("set2:{}",set2);
        log.info("set3:{}",set3);
    }
    public void c(){
        ZSetOperations<String, String> zset = stringRedisTemplate.opsForZSet();
        Set<String> set1 = zset.difference("B","C");
        Set<String> set2 = zset.union("B","C");
        Set<String> set3 = zset.intersect("B","C");
        log.info("set1:{}",set1);
        log.info("set2:{}",set2);
        log.info("set3:{}",set3);
    }
}
